package com.jiangxjsy.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.jiangxjsy.admin.service.AdminUserService;
import com.jiangxjsy.core.page.BasePage;
import com.jiangxjsy.core.wrap.Result;
import com.jiangxjsy.core.wrap.ResultBuilder;
import com.jiangxjsy.model.admin.bo.AdminUserBO;
import com.jiangxjsy.model.admin.vo.AdminSuperUserVO;
import com.jiangxjsy.model.admin.vo.AdminUserVO;
import com.jiangxjsy.model.admin.vo.DeptUserListVO;
import com.jiangxjsy.model.authorization.bo.AdminUserStatusBO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/6/6 14:42
 */

@RestController
@RequestMapping("/adminUser")
public class AdminUserController {
    @Resource
    private AdminUserService adminUserService;

    /**
     * 通过用户名查询用户
     *
     * @param username 用户名
     * @return Result
     */
    @RequestMapping("/findByUsername")
    public Result<List<Map<String, Object>>> findByUsername(String username) {
        List<Map<String, Object>> userInfoList = adminUserService.findByUsername(username);
        return ResultBuilder.ok(userInfoList);
    }

    /**
     * 通过用户 ID 查询用户角色 ID 列表
     *
     * @param userId 用户 ID
     * @return Result
     */
    @RequestMapping("/queryUserRoleIds")
    public Result<List<Integer>> queryUserRoleIds(@RequestParam("userId") @NotNull Long userId) {
        return ResultBuilder.ok(adminUserService.queryUserRoleIds(userId));
    }

    /**
     * 设置用户状态
     *
     * @param adminUserStatusBO 用户状态 BO 对象
     * @return Result
     */
    @PostMapping("/setUserStatus")
    public Result<?> setUserStatus(@RequestBody AdminUserStatusBO adminUserStatusBO) {
        adminUserService.setUserStatus(adminUserStatusBO);
        return ResultBuilder.ok();
    }

    /**
     * 激活用户
     *
     * @param adminUserStatusBO 用户状态 BO 对象
     * @return Result
     */
    @PostMapping("/activateUser")
    public Result<?> activateUser(@RequestBody AdminUserStatusBO adminUserStatusBO) {
        adminUserService.activateUser(adminUserStatusBO);
        return ResultBuilder.ok();
    }

    /**
     * 查询系统状态
     *
     * @return Result
     */
    @PostMapping("/querySystemStatus")
    public Result<Integer> querySystemStatus() {
        Integer status = adminUserService.querySystemStatus();
        return ResultBuilder.ok(status);
    }

    /**
     * 查询当前登录用户
     *
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return Result
     */
    @PostMapping("/queryLoginUser")
    public Result<AdminUserVO> queryLoginUser(HttpServletRequest request, HttpServletResponse response) {
        AdminSuperUserVO adminUser = adminUserService.queryLoginUser(request, response);
        return ResultBuilder.ok(adminUser);
    }

    /**
     * 通过条件分页查询员工列表
     *
     * @param adminUserBO 查询参数
     * @return Result
     */
    @PostMapping("/queryUserList")
    public Result<BasePage<AdminUserVO>> queryUserList(@RequestBody AdminUserBO adminUserBO) {
        return ResultBuilder.ok(adminUserService.queryUserList(adminUserBO));
    }

    /**
     * 查询员工状态人数
     *
     * @return Result
     */
    @PostMapping("/countNumOfUser")
    public Result<JSONObject> countUserByLabel() {
        return ResultBuilder.ok(adminUserService.countUserByLabel());
    }

    /**
     * 查询部门的用户列表（表单使用）
     *
     * @param deptId 部门 ID
     * @return Result
     */
    @PostMapping("/queryDeptUserList/{deptId}")
    public Result<DeptUserListVO> queryDeptUserList(@PathVariable Integer deptId) {
        DeptUserListVO deptUserListVO = adminUserService.queryDeptUserList(deptId, true);
        return ResultBuilder.ok(deptUserListVO);
    }

    /**
     * 新增用户
     *
     * @param adminUserVO 用户信息
     * @return Result
     */
    @PostMapping("/addUser")
    public Result<?> addUser(@RequestBody AdminUserVO adminUserVO, HttpServletRequest request, HttpServletResponse response) {
        adminUserService.addUser(adminUserVO, request, response);
        return ResultBuilder.ok();
    }
}
